import qs from 'query-string';
// eslint-disable-next-line import/no-cycle
import { ParamsRule } from '@/types/global';
import request, { Method } from '@/utils/axios';

interface loginParams {
  username: string | number;
  password: string;
}

/**
 * 登录
 * @returns
 * @param params
 */
// export function login(params: loginParams) {
//   return request({
//     url: '/passport/login/userLogin',
//     method: Method.POST,
//     needToken: false,
//     data: qs.stringify(params),
//     headers: {
//       'clientType': 'PC',
//       'Content-Type': 'application/x-www-form-urlencoded',
//     },
//   });
// }
export function login(username, password) {
  return request({
    url: '/passport/login/login',
    needToken: false,
    method: Method.POST,
    data: {
      username,
      password,
    },
  });
}

/**
 * 登出
 */
export function logoutUser() {
  return request({
    url: '/passport/login/logout',
    method: Method.POST,
    needToken: true,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

/**
 * 获取店铺信息
 */
export async function getStoreInfo() {
  // 获取用户信息
  const userInfoResponse = await request({
    url: '/passport/login/getInfo',
    method: Method.GET,
    needToken: true,
  });

  // 处理用户信息
  const userInfo = userInfoResponse.data.result.user;

  // 获取店铺设置
  const storeInfoResponse = await request({
    url: '/settings/storeSettings',
    method: Method.GET,
    needToken: true,
  });

  // 合并用户信息和店铺设置
  storeInfoResponse.data.result = {
    ...userInfo,
    ...storeInfoResponse.data.result,
  };

  return storeInfoResponse;
}
// export async function getStoreInfo() {
//   const userInfo = await request({
//     url: '/passport/login/getInfo',
//     method: Method.GET,
//     needToken: true,
//   }).then((response) => {
//     response.data.result = response.data.result.user;
//     return response;
//   });
//   return request({
//     url: '/settings/storeSettings',
//     method: Method.GET,
//     needToken: true,
//   }).then((response) => {
//     response.data.result = { ...response.data.result, ...userInfo };
//     return response;
//   });
// }
// export function getUserInfo() {
//   return request({
//     url: '/passport/login/getInfo',
//     method: Method.GET,
//     needToken: true,
//   }).then((response) => {
//     response.data.result = response.data.result.user;
//     return response;
//   });
// }
/**
 * 保存店铺信息
 */
export function setStoreInfo(params: any) {
  return request({
    url: '/settings/storeSettings',
    method: Method.PUT,
    data: qs.stringify(params),
    needToken: true,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

/**
 * 刷新token
 */
export function refreshTokenMethod(token: string) {
  return request({
    url: `/passport/login/refresh/${token}`,
    method: Method.GET,
    params: token,
  });
}

/**
 *
 * 个人中心修改密码
 */
export function changePassword(params: any) {
  return request({
    url: `/passport/login/modifyPass`,
    method: Method.POST,
    needToken: true,
    data: qs.stringify(params),
  });
}

/**
 *
 * 获取权限菜单
 */
export function getPermissionMenu() {
  return request({
    url: `/store/menu/memberMenu`,
    method: Method.GET,
    needToken: true,
  });
}

// 获取门户地址
export function getTongYiSystemUrl() {
  return request({
    url: '/passport/login/getTongYiSystemUrl',
    method: Method.GET,
    needToken: false,
  });
}

// 获取多机构
export function tempTokenGetDeptList(data) {
  return request({
    url: '/passport/login/tempTokenGetDeptList',
    method: Method.POST,
    data,
  });
}

// 多机构选择登录
export function tempTokenGetToken(data) {
  return request({
    url: '/passport/login/tempTokenGetToken',
    method: Method.POST,
    data,
  });
}

// 查询用户拥有的店铺
export function selectUserHaveSmall(data) {
  return request({
    url: '/passport/login/selectUserHaveShopStore',
    method: Method.POST,
    needToken: true,
    data,
  });
}
// 切换店铺
export function changeSmallApp(data) {
  return request({
    url: '/passport/login/changeShopStore',
    method: Method.POST,
    needToken: true,
    data,
  });
}
